package com.graphhopper.util;

import com.graphhopper.GHRequest;
import com.graphhopper.util.buffer.*;
import com.graphhopper.util.obstacle.AirObstacleUtils;
import com.graphhopper.util.shapes.GHPoint;
import com.graphhopper.vo.ObstacleVo;
import com.graphhopper.json.Statement;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.operation.buffer.BufferOp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * uam 路线规划查询CustomModelUtils 工具类
 *
 * @author Administrator
 */
public class CustomModelUtils {
    //缓冲区单位 米
    private static double bufferSize=30;

    private static LineBufferImpl lineBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();
    private static PolygonBufferImpl polygonBuffer = (PolygonBufferImpl) PoiTypeEnums.POLYGON.getBufferUtil();
    private static EnvelopeBufferImpl envelopeBuffer = (EnvelopeBufferImpl) PoiTypeEnums.RECTANGLE.getBufferUtil();

    public static void setCustomModel(GHRequest request, int key) {
        List<ObstacleVo> obstacleVos = new ArrayList<>();

        List<ObstacleVo> obstacle = request.getObstacleVos();
        if (!obstacle.isEmpty() && obstacle.size() > 0) {
            obstacleVos.addAll(obstacle);
        }

        CustomModel customModel = request.getCustomModel() == null ? new CustomModel() : request.getCustomModel();
        List<Statement> listProp = customModel.getPriority();
        JsonFeatureCollection collection = customModel.getAreas();
        int row = listProp.size();
        int rowIndex = obstacleVos.size();
        for (int i = 0; i < rowIndex; i++) {
            ObstacleVo obstacleVo = obstacleVos.get(i);
            String gps = obstacleVo.getPoints();

            String uuId = key+"_"+i;

            Statement statement = Statement.If("in_" + uuId, Statement.Op.MULTIPLY, "0");
            listProp.add(row, statement);

            JsonFeature jsonFeature = new JsonFeature();
            jsonFeature.setGeometry(lineBuffer.addBufferPolygon(gps, 1D));
            jsonFeature.setId(uuId);
            collection.getFeatures().add(jsonFeature);
        }
        request.setCustomModel(customModel);
    }

    private static Geometry getReqGeometry(List<GHPoint> points) {
        List<Double> lonList = new ArrayList<>(64);
        List<Double> latList = new ArrayList<>(64);
        points.forEach(a -> {
            lonList.add(a.lon);
            latList.add(a.lat);
        });
        double minLon = Collections.min(lonList);
        double maxLon = Collections.max(lonList);
        double minLat = Collections.min(latList);
        double maxLat = Collections.max(latList);
        Envelope envelope = new Envelope(minLon, maxLon, minLat, maxLat);
        return envelopeBuffer.getBuff(BufferBase.geometryFactory.toGeometry(envelope), BufferOp.CAP_SQUARE, bufferSize);
    }

    public static void setCustomModelZRYT(GHRequest request, int key){
        setCustomModel(request,key);


    }
}
